package action;

import com.github.pagehelper.PageInfo;
import entity.Tourist;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import service.TouristService;
import service.impl.TouristServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@WebServlet("/TouristServlet")
public class TouristAction extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private TouristService touristService = new TouristServiceImpl();

    public TouristAction() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String method = request.getParameter("method");
        method = method != null && method.length() > 0 ? method : "touristPageList";
        switch (method) {
            case "touristPageList":
                touristPageList(request, response);
                break;
            case "touristInsertSave":
                touristInsertSave(request, response);
                break;
            case "touristUpdate":
                touristUpdate(request, response);
                break;
            case "touristUpdateSave":
                touristUpdateSave(request, response);
                break;
            case "touristDelete":
                touristDelete(request, response);
                break;
            default:
                touristPageList(request, response);
                break;
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    protected void touristPageList(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Tourist tourist = new Tourist();
            Map<String,String[]> map = request.getParameterMap();
            BeanUtils.populate(tourist,map);
            PageInfo<Tourist> pageInfo = touristService.getPageList(tourist);
            request.setAttribute("pageInfo", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("tourist_list.jsp").forward(request, response);
    }

    protected void touristInsertSave(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Tourist tourist = Tourist.builder().build();
            DateConverter dateConverter = new DateConverter();
            dateConverter.setPattern("yyyy-MM-dd'T'HH:mm");
            ConvertUtils.register(dateConverter,Date.class);
            Map<String,String[]> map = request.getParameterMap();
            BeanUtils.populate(tourist,map);
            touristService.insert(tourist);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("TouristServlet");
    }

    protected void touristUpdate(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            request.setAttribute("tourist", touristService.getOne(Integer.valueOf(request.getParameter("id"))));
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("tourist_update.jsp").forward(request, response);
    }

    protected void touristUpdateSave(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Tourist tourist = new Tourist();
            Map<String,String[]> map = request.getParameterMap();
            BeanUtils.populate(tourist,map);
            touristService.update(tourist);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("TouristServlet");
    }

    protected void touristDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        id = id != null && id.length() > 0 ? id : "0";
        try {
            touristService.delete(Integer.valueOf(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("TouristServlet");
    }

}